package bible.sunflower.work.yinfeng.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONUtil4Hoyi {
	
    private static String TAG = JSONUtil4Hoyi.class.getSimpleName();
	
	public static List<Map<String,Object>> getList(String data,List<String> paralist){
		
		List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
		HashMap<String,Object> map;
		
		JSONObject jsonObj=JSONObject.fromObject(data);
		String tag = "result";
		String prefix = "data\":{\"";
		int index = data.indexOf( prefix ) + prefix.length();
		
		
		System.out.println(index+"index is what");
		System.out.println(data+"data is what");
		tag = data.substring(index, data.indexOf("\"", index) );
		System.out.println("json tag is " + tag );
		Object tmp2=jsonObj.getJSONObject("data").get( tag);
		System.out.println("json class is " + tmp2.getClass().getName() );
		if(JSONArray.class.getName().equals(tmp2.getClass().getName())){
			 JSONArray jsonArray =(JSONArray)tmp2;
			 System.out.println("array size is " + jsonArray.size() );
			 for(int i=0;i<jsonArray.size();i++){
				 Object obj=jsonArray.get(i);
				 JSONObject tmp4=(JSONObject)obj;
				 
				 map=new HashMap<String, Object>();
				 
				 if( paralist==null || paralist.size() == 0 ){
					 Iterator  keyIter = tmp4.keys();
					 while(keyIter.hasNext()){
				    	 String key = (String)keyIter.next();
				         Object value = tmp4.get(key);
				         map.put(key, value); 
				     }
				 }else{
					 for( String param : paralist ){
						 map.put(param, tmp4.get(param) );
					 }
				 }				 
				 list.add(map);
				 
			 }
		}else if(JSONObject.class.getName().equals(tmp2.getClass().getName())) {
			JSONObject obj =(JSONObject)tmp2;
			list.add( getObject( obj, paralist) );
		}
		return list;
	}
	
	public static Map<String, Object> getObject(String data, List<String> paralist){
		
		JSONObject jsonObj=JSONObject.fromObject(data);
		String tag = "result";
		String prefix = "data\":{\"";
		int index = data.indexOf( prefix ) + prefix.length();
		
		tag = data.substring(index, data.indexOf("\"", index) );
		System.out.println("json tag is " + tag );
		Object tmp2=jsonObj.getJSONObject("data").get( tag);
		JSONObject obj =(JSONObject)tmp2;
		
		return getObject(obj, paralist);
	}
	
	private static Map<String, Object> getObject(JSONObject jsonObj, List<String> paralist ){
		Map<String,Object> map=new HashMap<String, Object>();
		
		if( paralist==null || paralist.size() == 0 ){
			 Iterator  keyIter = jsonObj.keys();
			 while(keyIter.hasNext()){
		    	 String key = (String)keyIter.next();
		         Object value = jsonObj.get(key);
		         map.put(key, value); 
		     }
		 }else{
			 for( String param : paralist ){
				 map.put(param, jsonObj.get(param) );
			 }
		 }
		
		return map;
	}
		
	/*
	private static boolean isJSONObject(Object objStr) {
		// TODO Auto-generated method stub
		if(JSONObject.class.getName().equals(objStr.getClass().getName())){
			return true;
		}
		return false;
	}
     */
	private static JSONObject getJSONObjectByTag(JSONObject obj, String tag){
		JSONObject jsonObj = null;
		if( obj != null && tag != null && tag.length() > 0 ){
			Object tagObj = obj.get( tag );
			if( tagObj != null ){
				if(JSONObject.class.getName().equals(tagObj.getClass().getName())) {
					jsonObj = (JSONObject)tagObj;
				}else if(JSONArray.class.getName().equals(tagObj.getClass().getName())){
					JSONArray jsonArray=(JSONArray)tagObj;
					if( !jsonArray.isEmpty() ){
						jsonObj = jsonArray.getJSONObject(0);
					}
				}
			}
		}
		return jsonObj;
	}
	
	private static void parseJSONObject(JSONObject jsonObj, List<String> paramlist, Map<String,Object> valueMap, String paramPrefix ){		
		if( paramlist ==null || paramlist.size() == 0 ){
			 Iterator  keyIter = jsonObj.keys();
			 while(keyIter.hasNext()){
		    	 String key = (String)keyIter.next();
		         Object value = jsonObj.get(key);
		         valueMap.put(paramPrefix + key, value); 
		     }
		 }else{
			 Map<String, List<String>> paramMap = new HashMap<String,List<String>>();
			 for( String param : paramlist ){
				int index = param.indexOf("/");
				if( index < 0 ){
					valueMap.put( paramPrefix + param, jsonObj.get(param ) );
				}else{
					String key = param.substring(0, index);
					if( paramMap.containsKey( key ) ){
						List<String> subParamList = paramMap.get(key);
						subParamList.add( param.substring( index + 1 ) );
					}else{
						List<String> subParamList = new ArrayList<String>();
						paramMap.put(key, subParamList );
						subParamList.add( param.substring( index + 1 ) );
					}
				}
			 }
			 if( paramMap.size() > 0 ){
				 for(String key : paramMap.keySet() ){
					 JSONObject obj4Json = getJSONObjectByTag( jsonObj, key );
					 if( obj4Json != null ){
						 parseJSONObject( obj4Json, paramMap.get(key), valueMap, paramPrefix + key + "/" );
					 }
				 }
			 }
		 }
	}
	
	//解析分页返回的数据
	public static Map<String, Object> getListWithPage(String data, List<String> paramList, String pageTag, String listTag){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
		
		
		JSONObject jsonObj=JSONObject.fromObject(data);
		Object pageTmp=jsonObj.getJSONObject("data").get(pageTag);
		JSONObject obj =(JSONObject)pageTmp;
		
		boolean isLast=Boolean.parseBoolean(obj.getString("isLast"));
		boolean isFirst=Boolean.parseBoolean(obj.getString("isFirst"));
		Integer begin=obj.getInt("begin");
		
		
		
		
		
		
		Object listTmp=jsonObj.getJSONObject("data").get(listTag);
		
		if(JSONArray.class.getName().equals(listTmp.getClass().getName())){
			JSONArray jsonArray=(JSONArray)listTmp;
			for(int i=0;i<jsonArray.size();i++){
				 Object obj1=jsonArray.get(i);
				 JSONObject tmp4=(JSONObject)obj1;
				 Map<String, Object> valueMap = new HashMap<String,Object>();
				 parseJSONObject( tmp4, paramList, valueMap, "" );
				 list.add( valueMap );				 
			}
		}else if(JSONObject.class.getName().equals(listTmp.getClass().getName())) {
			JSONObject obj4json =(JSONObject)listTmp;
			Map<String, Object> valueMap = new HashMap<String,Object>();
			parseJSONObject( obj4json, paramList, valueMap, "" );
			list.add( valueMap );
		}	
		
		Integer size;
		if(obj.getString("size")!=null&&!"".equals(obj.getString("size"))){
			size=obj.getInt("size");
		}else{
			size=list.size();
			System.out.println(list.size()+ " list.size() " );
		}
		map.put("list", list);		
		return map;
	}
	
	/**
	 * 没有分页（page）解析返回的数据
	 * @param data
	 * @param paramList
	 * @param listTag
	 * @return
	 */
	public static Map<String, Object> getListWithPage(String data, List<String> paramList,String listTag){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
		
		
		JSONObject jsonObj=JSONObject.fromObject(data);
		
		
		Object listTmp=jsonObj.getJSONObject("data").get(listTag);
		
		if(JSONArray.class.getName().equals(listTmp.getClass().getName())){
			JSONArray jsonArray=(JSONArray)listTmp;
			for(int i=0;i<jsonArray.size();i++){
				 Object obj1=jsonArray.get(i);
				 JSONObject tmp4=(JSONObject)obj1;
				 Map<String, Object> valueMap = new HashMap<String,Object>();
				 parseJSONObject( tmp4, paramList, valueMap, "" );
				 list.add( valueMap );				 
			}
		}else if(JSONObject.class.getName().equals(listTmp.getClass().getName())) {
			JSONObject obj4json =(JSONObject)listTmp;
			Map<String, Object> valueMap = new HashMap<String,Object>();
			parseJSONObject( obj4json, paramList, valueMap, "" );
			list.add( valueMap );
		}	

		map.put("list", list);		
		return map;
	}

}
